//
//  HomeController.swift
//  swift-微博
//
//  Created by chenrin on 2016/12/13.
//  Copyright © 2016年 zhuoheng. All rights reserved.
//

import UIKit

private let IdentifierCell = "IdentifierCell"

class HomeController: BaseController {

    //保存微博数组
    var statuses : [HomeStatus]?{
        didSet{
            tableView.reloadData()
        }
    }
    
  //执行菜单动画
    lazy var popoverAnimator : PopoverAnimator = {
        let pa = PopoverAnimator()
        pa.presentFrame = CGRect(x: UIScreen.main.bounds.width / 2 - 100, y: 56, width: 200, height: 350)
        return pa
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //没有登录加载的界面
        userLogin = true
        if !userLogin {
            viditorView?.setupVisitorInfo(isHome: true, imageName: "visitordiscover_feed_image_house", message: "关注一些人,回这里看看")
            return
        }
        setupfNav()
        loadData()
        //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(changeTitleButton), name: NSNotification.Name(rawValue: popoverAnmiatorShow), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(changeTitleButton), name: NSNotification.Name(rawValue: popoverAnmiatorDismiss), object: nil)
        
    }
}

// MARK:- UI设置
extension HomeController{
    
    //设置导航栏
    fileprivate func setupfNav(){
        //初始化item
         navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(leftBarButtonItem))
         navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBarButtonItem))
        //初始化标题
        let titleBtn = TitleButton()
        titleBtn.setTitle("首页 ", for: .normal)
        titleBtn.addTarget(self, action: #selector(titleBtn(btn:)), for: .touchUpInside)
        navigationItem.titleView = titleBtn
        
        //注册一个cell
        tableView.register(HomeControllerCell.self, forCellReuseIdentifier: IdentifierCell)
       tableView.separatorStyle = .none
        
    }
}

// MARK:- 加载微博数据
extension HomeController{
    fileprivate func loadData(){
        HomeStatus.loadStatues { (models, error) in
            self.statuses = models
        }
    }
}

// MARK:- 回调方法
extension HomeController{
    func leftBarButtonItem() {
        tableView.reloadData()
    }
    func rightBarButtonItem() {
        let sb = UIStoryboard(name: "QRCode", bundle: nil)
        let sbVc = sb.instantiateInitialViewController()
        present(sbVc!, animated: true, completion: nil)
    }
    func titleBtn(btn:UIButton) {
       
       
        //弹出菜单
        let sb = UIStoryboard(name: "Popover", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        //设置转场代理
        //如果自定义转场动画，那么就不会将之前的那个移除
        vc?.transitioningDelegate = popoverAnimator
        vc?.modalPresentationStyle = UIModalPresentationStyle.custom
       present(vc!, animated: true, completion: nil)
        
    }
    //通知的方法
    func changeTitleButton() {
         //修改箭头方向
       let titleBtn = navigationItem.titleView as! TitleButton
        titleBtn.isSelected = !titleBtn.isSelected
    }
    
}

extension HomeController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.获取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: IdentifierCell, for: indexPath) as! HomeControllerCell
        cell.homeStatus = statuses![indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
       
        return cellAutoLayoutHeight(statues: statuses![indexPath.row]) + 70 + 44
    }
    
    ///计算文字的高度
    private func cellAutoLayoutHeight(statues:HomeStatus) -> CGFloat{
        let text = statues.text! as NSString
        let size = CGSize(width: UIScreen.main.bounds.size.width - 20, height: 10000)
        let rect  = text.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15)], context: nil)
        return rect.height
    }
}




